package cs236369.hw5.axis2.ypages;

import java.io.IOException;
import java.rmi.RemoteException;
import java.util.Arrays;

import org.apache.axis2.AxisFault;
import org.apache.log4j.Logger;

import cs236369.hw5.axis2.ypages.RegistrationServiceServiceStub.AddEndpoint;
import cs236369.hw5.axis2.ypages.RegistrationServiceServiceStub.AddEndpointResponse;
import cs236369.hw5.axis2.ypages.RegistrationServiceServiceStub.DeleteEndpoint;
import cs236369.hw5.axis2.ypages.RegistrationServiceServiceStub.DeleteEndpointResponse;
import cs236369.hw5.axis2.ypages.RegistrationServiceServiceStub.GetRegisteredEndpoints;
import cs236369.hw5.axis2.ypages.RegistrationServiceServiceStub.GetRegisteredEndpointsResponse;

/**
 * CS236369 Spring 2009, IIT:
 * 
 * This is a sample of a client that uses the registration service of the
 * course. Note the calling <code>getRegisteredEndpoints</code> returns an array
 * of Strings. You may assume this array size is no greater than 100. (We did
 * this to keep it simple by returning Strings)
 */

public class YellowPagesClient
{

	public YellowPagesClient()
	{

	}

	/**
	 * 
	 * @param service is the service url, example:
	 *            http://ibm411.cs.technion.ac.il/HW5/services/RegistrationService
	 * @return list of all the registered endpoints.
	 */
	public String[] getEndPoints()
	{
		Logger logger = Logger.getLogger(getClass());
		RegistrationServiceServiceStub stub = null;
		try
		{
			String service = YellowPagesConf.getService();
			logger.info("generating stub for: " + service);
			stub = new RegistrationServiceServiceStub(service);
			GetRegisteredEndpoints getRegisteredEndpoints = new GetRegisteredEndpoints();
			logger.debug("getting end points.");
			GetRegisteredEndpointsResponse registeredEndpointsResponse = stub
			    .getRegisteredEndpoints(getRegisteredEndpoints);

			String[] res = registeredEndpointsResponse.getGetRegisteredEndpointsReturn();
			logger.debug("response is: " + Arrays.toString(res));
			return res;
		}
		catch (AxisFault e)
		{
			logger.error("Axis exception", e);
		}
		catch (RemoteException e)
		{
			logger.error("Remote exception", e);
		}
		catch (IOException e)
		{
			logger.error("IO Exception", e);
		}
		return null;

	}

	/**
	 * register to the yellow pages service
	 */
	public void registerToService()
	{
		Logger logger = Logger.getLogger(getClass());
		RegistrationServiceServiceStub stub = null;
		try
		{
			String service = YellowPagesConf.getService();
			String client = YellowPagesConf.getClient();
			logger.info("generating stub for: " + service);
			stub = new RegistrationServiceServiceStub(service);
			AddEndpoint addEP = new AddEndpoint();
			logger.debug("adding url: " + client);
			addEP.setUrl(client);
			AddEndpointResponse addEPResponse = stub.addEndpoint(addEP);
			logger.info("AddEndpoint method returned: " + addEPResponse.getAddEndpointReturn());
		}
		catch (AxisFault e)
		{
			logger.error("Axis exception", e);
		}
		catch (RemoteException e)
		{
			logger.error("Remote exception", e);
		}
		catch (IOException e)
		{
			logger.error("IO Exception", e);
		}
	}

	/**
	 * unregister url from service
	 */
	public void unregisterFromService()
	{
		Logger logger = Logger.getLogger(getClass());
		RegistrationServiceServiceStub stub = null;
		try
		{
			String service = YellowPagesConf.getService();
			String client = YellowPagesConf.getClient();

			logger.info("generating stub for: " + service);
			stub = new RegistrationServiceServiceStub(service);
			DeleteEndpoint deleteEP = new DeleteEndpoint();
			logger.debug("adding url: " + client);
			deleteEP.setUrl(client);
			DeleteEndpointResponse delResponse = stub.deleteEndpoint(deleteEP);

			logger.info("DeleteEndpoint method returned: " + delResponse.getDeleteEndpointReturn());
		}
		catch (AxisFault e)
		{
			logger.error("Axis exception", e);
		}
		catch (RemoteException e)
		{
			logger.error("Remote exception", e);
		}
		catch (IOException e)
		{
			logger.error("IO Exception", e);
		}

	}

}
